<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
      table {
        border-collapse: collapse;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <!-- 
      [
        {
          name:"张三",
          age:20,
          tel:180225151515
        },
        {
          name:"张三",
          age:20,
          tel:180225151515
        }
      ]
      <my-table :data=""></my-table>
      -->
      <my-table :data="list" :map="map">
        <template #default="props">
          <template v-if="props.key=='tel'">
            <span style="background-color: green">{{props.val}}</span>
          </template>
          <template v-else-if="props.key=='name'">
            <span style="font-size: 30px">{{props.val}}</span>
          </template>
          <template v-else-if="props.key=='tool'">
            <button>修改</button>
            <button @click="list.splice(props.index,1)">删除</button>
          </template>
          <template v-else>
            <span>{{props.val}}</span>
          </template>
        </template>
      </my-table>
    </div>
  </body>
  <script>
    let app = Vue.createApp({
      data() {
        return {
          list: [
            {
              name: "张三",
              age: 20,
              tel: 180225151515,
            },
            {
              name: "李四",
              age: 30,
              tel: 180225151515,
            },
          ],
          map: {
            name: "姓名",
            age: "年龄",
            tel: "手机",
            tool: "操作",
          },
        };
      },
    });
    // 注册组件
    app.component("my-table", {
      template: `
        <table border="1" cellpadding="10">
          <tr>
            <th v-for="val in map">{{val}}</th>  
          </tr>  
          <tr v-for="(item,index) in data">
            <td v-for="(val,key) in map">
              <slot v-if="$slots.default" :val="item[key]" :key="key" :index="index"></slot>
              <span v-else>{{item[key]}}</span>
            </td>  
          </tr> 
        </table>
      `,
      props: ["data", "map"],
      data() {
        return {};
      },
    });
    app = app.mount("#app");
  </script>
</html>
